#include "ship.h"
#include "game.h"
#include "texture_manager.h"

Ship::Ship(unsigned int size, Direction direction, Owner owner) 
    : m_size(size), m_direction(direction), m_owner(owner) {
    m_life = size;
    m_placed = false;
    m_position = new Coord(0, 0);
}

void Ship::changeDirection() {
    m_direction = (m_direction == TOP_DOWN ? LEFT_RIGHT : TOP_DOWN);
}

Vector2D Ship::increment(Direction direction) {
    return direction == TOP_DOWN ? Vector2D(0, 1) : Vector2D(1, 0);
}

Vector2D Ship::increment() const {
    return increment(m_direction);
}

void Ship::decLife() {
    if (alive())
        m_life--;
}

bool Ship::alive() const {
    return m_life > 0;
}

bool Ship::placed() const {
    return m_placed;
}

void Ship::placed(bool b) {
    m_placed = b;
}

void Ship::position(int cx, int cy) {
    m_position->x = cx;
    m_position->y = cy;
}

void Ship::draw() {
    std::string textureID = "ship" + std::to_string(static_cast<long long>(size()));
    int x, y, alpha;
    double angle;
    if (direction() == LEFT_RIGHT) {
        x = m_position->x * 35 + (owner() == PLAYER ? 25 : 425);
        y = m_position->y * 35 + 150;
        angle = 0;
    } else {
        x = static_cast<int>((m_position->x - (size() / 2.0 - 0.5)) * 35 + (owner() == PLAYER ? 25 : 425));
        y = static_cast<int>((m_position->y + (size() / 2.0 - 0.5)) * 35 + 150);
        angle = 90;
    }
    if (owner() == PLAYER) {
        if (placed())
            alpha = 255;
        else
            alpha = 127;
    } else {
        if (!alive())
            alpha = 255;
        else
            alpha = 0;
    }
    TheTextureManager::Instance()->drawFrame(textureID, x, y, 100 * size(), 100, 0, 0, TheGame::Instance()->getRenderer(), angle, alpha, 0.35);
}

void Ship::update() {
}
